import axios from "axios"
import type { AxiosInstance } from "axios"
import '@/mock/student'
import '@/mock/teacher'
import '@/mock/login'
import '@/mock/admin'
import '@/mock/banner'
import '@/mock/chair'
import '@/mock/audition'
import '@/mock/coupon'
import '@/mock/class'
const instance: AxiosInstance = axios.create({
    // 请求基准地址
    baseURL: "/",
    // 超时时间
    timeout: 60000,
})
// 添加请求拦截器
instance.interceptors.request.use(
    function (config) {
        if (config.url !== "/admin/login") {
            config.headers.token = window.localStorage.getItem("token")
        }
            return config
    },
    function (error) {
        return Promise.reject(error);
    })

instance.interceptors.response.use(
    function (response) {
        // if (response.data.code === '10119') {
        // }
        return response
    },
    function (error) {
        return Promise.reject(error)
    })

interface IOptions {
    url: string;
    method?: string;
    data?: any;
}

export default function ajax(options:IOptions) {
    const { url, method = "get", data = {} } = options
    const reg = /^get$/i
    if (reg.test(method)) {
        return instance.get(url, {
            params: data
        })
    } else {
        return instance.post(url, data)
    }
}